/**
 * This file is part of magnolia-module-bespin.
 * 
 * magnolia-module-bespin is free software: you
 * can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * 
 * magnolia-module-bespin is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU General
 * Public License along with magnolia-module-bespin.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

package com.nrgedge.magnolia.module.ace;

import info.magnolia.cms.core.Content;
import info.magnolia.cms.gui.control.Edit;
import info.magnolia.module.ModuleRegistry;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

/**
 * @author brian.bowman@nrg-edge.com
 * @author adam.galloway@nrg-edge.com
 */
public class AceEdit extends Edit {
	
	public AceEdit() {
	}

	public AceEdit(String name, String value) {
		super(name, value);
	}

	public AceEdit(String name, Content websiteNode) {
		super(name, websiteNode);
	}

	public String getHtml() {
		AceEditorModule module = (AceEditorModule) ModuleRegistry.Factory.getInstance().getModuleInstance("ace-editor");
		String theme = module.getTheme();
		
		String id = this.getId();
		StringBuffer html = new StringBuffer(); 
		
		if (id == null) {
			id = this.getName();
		}
		
		html.append("<div")
			.append(" id=\"" + id + "\"")
			.append(" style=\"display: inline-block; width: 100%; height: 480px;\"")
			.append(this.getHtmlCssClass())
			.append(this.getHtmlCssStyles())
			.append(getHtmlEvents())
			.append("></div>\n");
		
		
		html.append("<script type=\"text/javascript\">\n")
			.append("    var editor = ace.edit(\"" + id + "\");\n");
		
		if(StringUtils.isNotEmpty(theme)) {
			html.append("    editor.setTheme(\"ace/theme/" + theme + "\");\n");
		} else {
			html.append("    editor.setTheme(\"ace/theme/monokai\");\n");
		}
		
		html.append("    document.getElementById('" + id + "').style.fontSize='14px';")
		
			.append("    jQuery(window).resize(function(){\n")
			.append("        var sizeObj = MgnlDHTMLUtil.getWindowSize();\n") 
            .append("        var hTab = sizeObj.h - mgnlTabTop - mgnlTabBottom - 50;\n")
            .append("        var editorDiv = document.getElementById('" + id + "');\n")
            .append("        editorDiv.style.height = hTab + 'px';\n")
			.append("        editor.resize();\n")
			.append("    });\n")
			
			.append("    MgnlDHTMLUtil.addOnLoad(function(){\n")
			.append("        editor.setValue(\"" + StringEscapeUtils.escapeJavaScript(getValue()) + "\");\n")
			.append("        editor.gotoLine(1);\n")
			.append("        editor.scrollToLine(0);\n")
			.append("    });\n")
			.append("</script>\n"); //$NON-NLS-1$*/
		
		if (this.getSaveInfo()) {
			html.append(this.getHtmlSaveInfo());
		}
		
		return html.toString();
	}

}